﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Diagnostics;

namespace Interface_Design
{
    public partial class AppDock : Form
    {
        Point mouseOffset = new Point();
        private bool mouseDown = false;
        IniFile configINI;
        Timer appTimer = new Timer();

        List<String> wordImages = new List<String>{@"..\..\Images\wordIconMid.png", @"..\..\Images\wordIconMid2.png",
                                                   @"..\..\Images\wordIconSmall.png", @"..\..\Images\wordIconMid2.png",
                                                   @"..\..\Images\wordIconMid.png", @"..\..\Images\wordIcon.png"};
        List<String> powerpointImages = new List<String>{@"..\..\Images\powerpointIconMid.png", @"..\..\Images\powerpointIconMid2.png",
                                                         @"..\..\Images\powerpointIconSmall.png", @"..\..\Images\powerpointIconMid2.png",
                                                         @"..\..\Images\powerpointIconMid.png", @"..\..\Images\powerpointIcon.png"};
        

        public AppDock()
        {
            InitializeComponent();
            this.FormBorderStyle = FormBorderStyle.None;
            configINI = new IniFile(@"..\..\Configuration\interfaceConfig.ini");   
        }

        private void appDock_Load(object sender, EventArgs e)
        {
            this.BackColor = Color.Aqua;
            appDockPictureBox.BackColor = Color.Aqua;
            appPictureBox1.BackColor = Color.Aqua;
            this.TransparencyKey = Color.Aqua;

            int x = Int32.Parse(configINI.IniReadValue("ApplicationDock", "startX"));
            int y = Int32.Parse(configINI.IniReadValue("ApplicationDock", "startY"));
            this.Location = new Point(x, y);

            appTimer.Interval = 100;
            appTimer.Tick += new EventHandler(appTimer_Tick);
        }

        private void appDockPictureBox_MouseDown(object sender, MouseEventArgs e)
        {
            if (e.Button == MouseButtons.Left)
            {
                mouseOffset = new Point(-e.X - SystemInformation.FrameBorderSize.Width, -e.Y - SystemInformation.FrameBorderSize.Height);
                mouseDown = true;
            }
        }

        private void appDockPictureBox_MouseUp(object sender, MouseEventArgs e)
        {
            if (e.Button == MouseButtons.Left)
                mouseDown = false;
        }

        private void appDockPictureBox_MouseMove(object sender, MouseEventArgs e)
        {
            if (mouseDown)
            {
                Point mousePosition = Control.MousePosition;
                mousePosition.Offset(mouseOffset.X, mouseOffset.Y);
                this.Location = mousePosition;
            }
        }

        private void appPictureBox1_Click(object sender, EventArgs e)
        {
            if (!timerStarted)
            {
                appTimer.Tag = new App("WORD", "WINWORD.EXE");
                appTimer.Start();
                timerStarted = true;
            }
        }

        private void appPictureBox2_Click(object sender, EventArgs e)
        {
            if (!timerStarted)
            {
                appTimer.Tag = new App("POWERPOINT", "POWERPNT.EXE");
                appTimer.Start();
            }
        }

        bool timerStarted = false;
        int timerCounter = 0;
        private void appTimer_Tick(object sender, EventArgs e)
        {

            if (((App)appTimer.Tag).Name == "WORD")
            {
                changeImage(appPictureBox1, wordImages);
            }
            else if (((App)appTimer.Tag).Name == "POWERPOINT")
            {
                changeImage(appPictureBox2, powerpointImages);
            }
        }

        private void changeImage(PictureBox pictureBox, List<string> imagePaths)
        {
            pictureBox.Image = Image.FromFile(imagePaths[timerCounter]);
            timerCounter++;

            if (timerCounter == imagePaths.Count())
            {
                timerCounter = 0;
                appTimer.Stop();
                timerStarted = false;
                startProcess(((App)appTimer.Tag).PathToApp);
            }
        }

        private void startProcess(string processPath)
        {
            Process process = new Process();
            process.StartInfo.FileName = processPath;
            process.Start();
        }

        private void AppDock_FormClosing(object sender, FormClosingEventArgs e)
        {
            configINI.IniWriteValue("ApplicationDock", "startX", this.Location.X.ToString());
            configINI.IniWriteValue("ApplicationDock", "startY", this.Location.Y.ToString());
        }
        
    }
}
